1325D - Ehab the Xorcist - CodeForces Solution


bitmasks constructive algorithms greedy number theory *1700

Please click on ads to support us..

Python Code:

u,v = map(int,input().split())
if(u > v): print(-1)
elif (v-u)%2 == 1: print(-1)
else:
	x = (v-u)//2
	if(v == 0 and u == 0):
		print(0)
	elif(v == u):
		print(1)
		print(u)
	elif(x^u) == x+u:
		print(2)
		print(x+u,x)
	else:
		print(3)
		print(x,x,u)

C++ Code:

#include<bits/stdc++.h>
#define MOD 1000000007
#define MAX 200000
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	long long xr, sm;
	cin>>xr>>sm;
	if(xr>sm)
		cout<<"-1"<<endl;
	else if(xr==sm)	
	{
		if(sm)
			cout<<"1"<<endl<<xr<<endl;
		else cout<<"0"<<endl;
	}
	else if((sm-xr)%2==0)
	{
		//sm = a+b
		//xr = a^b
		//sm=xr+2(a&b) -> (sm-xr)/2 = a&b	
		long long ab = (sm-xr)/2,a=0,b=0;
		for(int i=0;i<62;++i)
		{
			if(ab&((1LL)<<i))
			{
				a |= ((1LL)<<i);
				b |= ((1LL)<<i);
			}
			if(xr&((1LL)<<i))
				a |= ((1LL)<<i);
		}
		if((a^b)==xr&&(a+b)==sm)
		{
			cout<<"2"<<endl;
			cout<<a<<" "<<b<<endl;
		}
		else
		{
			cout<<"3"<<endl;
			cout<<xr<<" "<<ab<<" "<<ab<<endl;		
		}
	}
	else
	{
		cout<<"-1"<<endl;
	}
	return 0;
}


Comments

Submit
0 Comments
More Questions

318. Maximum Product of Word Lengths
448. Find All Numbers Disappeared in an Array
1155. Number of Dice Rolls With Target Sum
415. Add Strings
22. Generate Parentheses
13. Roman to Integer
2. Add Two Numbers
515. Find Largest Value in Each Tree Row
345. Reverse Vowels of a String
628. Maximum Product of Three Numbers
1526A - Mean Inequality
1526B - I Hate 1111
1881. Maximum Value after Insertion
237. Delete Node in a Linked List
27. Remove Element
39. Combination Sum
378. Kth Smallest Element in a Sorted Matrix
162. Find Peak Element
1529A - Eshag Loves Big Arrays
19. Remove Nth Node From End of List
925. Long Pressed Name
1051. Height Checker
695. Max Area of Island
402. Remove K Digits
97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
501A - Contest
160A- Twins